﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Medianamik.Core;
using Medianamik.Globalization;

namespace Medianamik.Tests.Unit.Fakes
{
    class FakeMedianamikResourceDataManager : IMedianamikResourceDataManager
    {
       
        #region IMedianamikResourceDataManager Members

        private List<Resource> _resources = new List<Resource>();

        public IEnumerable<Resource> GetNewOrUpdatedResources(IEnumerable<string> cultureNames, DateTime lastModifiedOn, IEnumerable<string> resourceSets, int maxResourceUpdate)
        {
            throw new NotImplementedException();
        }

        public void UpdateOrAdd(Resource resource)
        {
            if (_resources.Contains(resource))
                _resources.Remove(resource);

            _resources.Add(resource);
        }

        public string ErrorMessage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        
        public Dictionary<string, string> GetResourceStrings(string resourceId, string resourceSet)
        {
            var resStrings = new Dictionary<string, string>();

            _resources.Where(x => x.Id == resourceId && x.ResourceSet == resourceSet)
                .ForEach(x => resStrings.Add(x.CultureName, x.Value));

            return resStrings;
        }

        #endregion

        #region IMedianamikResourceDataManager Members


        public IEnumerable<string> GetResourceIds(string resourceSet)
        {
            var ids = from r in _resources
                      where r.ResourceSet == resourceSet
                      select r.Id;
            
            var distincts = new List<string>();

            foreach (var id in ids)
                if (!distincts.Contains(id))
                    distincts.Add(id);

            return distincts;
        }

        public DataTable GetAllResourceSets(ResourceListingTypes type)
        {
            var sets = GetResourceSets(type);
            
            var dt = new DataTable("resourceSets");
            dt.Columns.Add("resourceSet");
            foreach(var set in sets)
                dt.Rows.Add(new object[] {set});

            return dt;
        }

        #endregion

        #region IMedianamikResourceDataManager Members


        public IEnumerable<string> GetResourceSets(ResourceListingTypes type)
        {
            var sets = new List<string>();

            foreach (var resource in _resources)
            {
                if (!sets.Contains(resource.ResourceSet))
                    sets.Add(resource.ResourceSet);
            }
            return sets;
        }

        #endregion

        #region IMedianamikResourceDataManager Members

        

        #endregion

        #region IMedianamikResourceDataManager Members


        public IEnumerable<Resource> GetResourceForLocalUseOnly()
        {
            return Enumerable.Empty<Resource>();
        }

        #endregion
    }
}